package com.sprouting.ops.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.model.OpsProduct;
import com.sprouting.ops.model.OpsQuickAccess;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.service.OpsAppService;
import com.sprouting.ops.service.OpsProductService;
import com.sprouting.ops.service.OpsQuickAccessService;
import com.sprouting.ops.service.OpsSiteService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 快捷入口;(OpsQuickAccess)(OPS_QUICK_ACCESS) 表控制层
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-10-12 22:15:42
 */
@Slf4j
@RestController
@RequestMapping("opsQuickAccess")
public class OpsQuickAccessController {

    @Resource
    private OpsQuickAccessService opsQuickAccessService;
    @Resource
    private OpsAppService opsAppService;
    @Resource
    private OpsSiteService opsSiteService;
    @Resource
    private OpsProductService opsProductService;

    /**
     * 获取可添加的应用
     * @return
     */
    @PostMapping("getByQuickApp")
    public ResultJson getByQuickApp(){
        String userId = StpUtil.getLoginIdAsString();
        return ResultJson.success(opsAppService.getByQuickApp(userId));
    }

    /**
     * 获取可添加的场所
     * @return
     */
    @PostMapping("getByQuickSite")
    public ResultJson getByQuickSite(){
        String userId = StpUtil.getLoginIdAsString();
        return ResultJson.success(opsSiteService.getByQuickSite(userId));
    }

    /**
     * 获取可添加的产品
     * @return
     */
    @PostMapping("getByQuickProduct")
    public ResultJson getByQuickProduct(){
        String userId = StpUtil.getLoginIdAsString();
        return ResultJson.success(opsProductService.getByQuickProduct(userId));
    }

    /**
     * 获取快捷入口
     * @param type 类型 {@link com.sprouting.ops.interfaces.ConstantBase.QuickAccessType}
     * @return
     */
    @PostMapping("getQuickList")
    public ResultJson getQuickList(Integer type){
        if (type == null){
            return ResultJson.errorCheck();
        }
        String userId = StpUtil.getLoginIdAsString();
        return ResultJson.success(opsQuickAccessService.getQuickList(userId, type));
    }

    /**
     * 删除快捷入口;
     * @param id 快捷入口;id
     * @return
     */
    @PostMapping("delOpsQuickAccess")
    @IdempotentSingle
    public ResultJson delOpsQuickAccess(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorCheck();
        }

        opsQuickAccessService.removeById(id);
        return ResultJson.success();
    }


    /**
     * 保存快捷入口;
     * @param bo 保存参数
     * @return
     */
    @PostMapping("saveOpsQuickAccess")
    @IdempotentSingle
    public ResultJson saveOpsQuickAccess(@Validated(AddGroup.class) OpsQuickAccess bo){
        String msg = checkOpsQuickAccess(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        bo.setId(IdTool.getId());
        bo.setUserId(StpUtil.getLoginIdAsString());
        EntityTool.saveBase(bo);
        opsQuickAccessService.save(bo);
        return ResultJson.success(bo);
    }


    /**
     * 快捷入口;数据校验
     * @param bo 快捷入口; 校验数据
     * @return 非null则有错误
     */
    private String checkOpsQuickAccess(OpsQuickAccess bo){

        return null;
    }

}
